package com.gov.dna.admin;


import com.baomidou.mybatisplus.annotations.TableName;
import com.gov.dna.admin.entity.RolePermissionScope;
import com.gov.dna.common.util.CodeGeneratorUtils;
import com.gov.dna.common.util.Field;
import com.gov.dna.common.util.GovStringUtils;
import io.swagger.annotations.ApiModel;

import java.lang.annotation.Annotation;
import java.util.List;

public class BuildSQL {
    public static void main(String[] args) {
        Class cls = RolePermissionScope.class;
        List<Field> fields = CodeGeneratorUtils.getFieldsByClass(cls);
        Annotation[] tn = cls.getAnnotationsByType(TableName.class);
        String tableName = ((TableName) tn[0]).value();
        Annotation[] tc = cls.getAnnotationsByType(ApiModel.class);
        String tableComment = ((ApiModel) tc[0]).value();
        getSQL(tableName, tableComment, fields);
        getMapper(fields);
        getWhereMapper(tableName, cls.getSimpleName(), fields);
    }

    public static void getMapper(List<Field> fields) {
        StringBuffer columnList = new StringBuffer();
        for (Field fd : fields) {
            String comment = fd.getDescription();
            String filed = fd.getDbFieldName();
            columnList.append("t.").append(filed).append(",");
            System.out.println(" <!--" + comment + "-->");
            System.out.println(" <result property=\"" + fd.getJavaFieldName() + "\" column=\"" + filed + "\"/>");
        }
        System.out.println("<sql id=\"Base_Column_List\">");
        System.out.println(GovStringUtils.removeBothEndsComma(columnList.toString()));
        System.out.println("</sql>");
    }

    public static void getWhereMapper(String tableName, String className, List<Field> fields) {
        System.out.println("<sql id=\"Base_Where_Clause\">");
        for (Field field : fields) {
            String comment = field.getDescription();
            String filed = field.getDbFieldName();
            if (field.getJavaFieldType().equals("Integer") || field.getJavaFieldType().equals("Long")) {
                System.out.println("<if test=\"param." + field.getJavaFieldName() + " != null\">");
                System.out.println(" AND t." + filed + " = #{param." + field.getJavaFieldName() + ",jdbcType=NUMERIC}");
                System.out.println("</if>");
            } else if (field.getJavaFieldType().equals("String")) {
                System.out.println("<if test=\"param." + field.getJavaFieldName() + " != null and param." + field.getJavaFieldName() + " != ''\">");
                System.out.println(" AND t." + filed + " = #{param." + field.getJavaFieldName() + ",jdbcType=VARCHAR}");
                System.out.println("</if>");
            }
        }
        System.out.println("</sql>");
        System.out.println("<select id=\"find\" resultMap=\"BaseResultMap\" parameterType=\"" + className + "\">");
        System.out.println("SELECT");
        System.out.println("<include refid=\"Base_Column_List\" />");
        System.out.println("FROM " + tableName + " t WHERE 1=1");
        System.out.println("<include refid=\"Base_Where_Clause\" />");
        System.out.println("</select>");
        System.out.println("<select id=\"findByPage\" resultMap=\"BaseResultMap\" parameterType=\"" + className + "\">");
        System.out.println("SELECT");
        System.out.println("<include refid=\"Base_Column_List\" />");
        System.out.println("FROM " + tableName + " t WHERE 1=1");
        System.out.println("<include refid=\"Base_Where_Clause\" />");
        System.out.println("</select>");

    }

    public static void getSQL(String tableName, String tableComment, List<Field> fields) {

        System.out.println("CREATE TABLE `" + tableName + "` (");

        for (Field field : fields) {
            if (field.getDbFieldName().equals("is_deleted")) {
                System.out.println(" `is_deleted` tinyint(1) DEFAULT '0' COMMENT '逻辑删除标志',");
            } else if (field.getDbFieldName().equals("id")) {
                System.out.println("`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',");
            } else if (field.getDbFieldName().equals("create_time") || field.getDbFieldName().equals("modified_time")) {
                System.out.println("`" + field.getDbFieldName() + "` " + field.getDbFieldType() + "  DEFAULT CURRENT_TIMESTAMP COMMENT '" + field.getDescription() + "',");
            } else {
                System.out.println("`" + field.getDbFieldName() + "` " + field.getDbFieldType() + "  DEFAULT NULL COMMENT '" + field.getDescription() + "',");
            }
        }
        System.out.println("PRIMARY KEY (`id`)");
        System.out.println(") ENGINE=InnoDB AUTO_INCREMENT=1 CHARSET=utf8mb4 COLLATE=utf8mb4_bin CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='" + tableComment + "';");

    }


}
